package com.myProject.serves.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myProject.bean.TArea;
import com.myProject.bean.TUser;
import com.myProject.dao.AreaDao;
import com.myProject.dao.UserDao;
import com.myProject.serves.UserSev;
import org.apache.ibatis.session.SqlSession;
import util.SqlSessionUtil;

import java.util.List;

public class UserSevImpl implements UserSev {
    @Override
    public List<TUser> getUserList(TUser tUser) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserDao ud = sqlSession.getMapper(UserDao.class);
        List<TUser> list = ud.getUserList(tUser);
        sqlSession.clearCache();
        return list;
    }

    @Override
    public List<TUser> loginUser(TUser tUser) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserDao ud = sqlSession.getMapper(UserDao.class);
        List<TUser> list = ud.loginUser(tUser);
        sqlSession.clearCache();
        return list;
    }

    @Override
    public PageInfo<TUser> showUserByPage(int pageNo, int pageSize, TUser tUser) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserDao ud = sqlSession.getMapper(UserDao.class);
        PageHelper.startPage(pageNo, pageSize);
        List<TUser> list = ud.getUserList(tUser);
        PageInfo<TUser> page = new PageInfo<TUser>(list);
        SqlSessionUtil.commit();
//      sqlSession.close();
        return page;
    }

    @Override
    public boolean addUser(TUser tUser) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserDao ud = sqlSession.getMapper(UserDao.class);
        int x = ud.addUser(tUser);
        SqlSessionUtil.commit();
        sqlSession.close();
        return x > 0 ? true : false;
    }

    @Override
    public boolean deleteUser(TUser tUser) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserDao ud = sqlSession.getMapper(UserDao.class);
        int x = ud.deleteUser(tUser);
        SqlSessionUtil.commit();
        sqlSession.close();
        return x > 0 ? true : false;
    }

    @Override
    public boolean updateUser(TUser tUser) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserDao ud = sqlSession.getMapper(UserDao.class);
        int x = ud.updateUser(tUser);
        SqlSessionUtil.commit();
        sqlSession.close();
        return x > 0 ? true : false;
    }

    @Override
    public List<TUser> loginUserName(TUser tUser) {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        UserDao ud = sqlSession.getMapper(UserDao.class);
        sqlSession.clearCache();
        return ud.loginUserName(tUser);
    }

}
